package com.his.server.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import com.his.server.consts.ServerConsts;
import com.his.server.dao.iface.UserInfoDAO;
import com.his.server.exception.DBException;
import com.his.server.utils.dao.DaoUtil;
import com.his.server.utils.log.DBSqlLog;
import com.his.server.utils.log.LogUtilFactory;
import com.his.server.utils.log.LogUtils;
import com.his.server.utils.main.EnvironmentProperties;

public class UserInfoDAOImpl implements UserInfoDAO {

	public String userName = null;
	public String password = null;
	private int usercount = 0;
	private ResultSet rs = null;
	private PreparedStatement pdst = null;
	private Properties pstmtProperties = null;
    private List sqlBindList = new ArrayList();
    
    private static final LogUtils log = LogUtilFactory.getLog(UserInfoDAOImpl.class);
	
	public UserInfoDAOImpl() {

	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public void setUserCount(int userCount) {
		this.usercount = userCount;
	}

	public String getUserName() {
		return this.userName;
	}

	public String getPassword() {
		return this.password;
	}

	public int getUserCount() {
		return this.usercount;
	}

	public void doSqlExecute (Connection conn) throws DBException {
		
	    if (log.isDebugEnabled()) {
	        log.writeDebug("doSqlExecute START execute() {");
	    }
	    
		pstmtProperties = EnvironmentProperties.getEproperties().getPropertyList().get(1);
		
		try {
			String strSql = pstmtProperties.getProperty(ServerConsts.QUERY_USER_INFO_SELECT);
			pdst = conn.prepareStatement(strSql);
			
			DaoUtil.setString(pdst, 1, this.getUserName());
			DBSqlLog.setSqlLogBind(sqlBindList, this.getUserName());
			DaoUtil.setString(pdst, 2, this.getPassword());
			DBSqlLog.setSqlLogBind(sqlBindList, this.getPassword());

			if (log.isDebugEnabled()) {
				log.writeDebug(DBSqlLog.editSqlLog(strSql, sqlBindList));
				sqlBindList.clear();
			}
			
			long startTime = 0;
			if (log.isDebugEnabled()) {
				startTime = System.currentTimeMillis();
			}
	            
			rs = pdst.executeQuery();

            long finishTime = 0;
            if (log.isDebugEnabled()) {
                finishTime = System.currentTimeMillis();
            }
            if (log.isDebugEnabled()) {
            	log.writeDebug("elapsed time: " + (finishTime - startTime) + "ms");
            }

			int i = 0;
			if (rs.next()) {
				this.setUserCount(DaoUtil.getInteger(rs, 1));
				i++;
			}
			
            if (log.isDebugEnabled()) {
            	log.writeDebug("result count: " + i);
            }
			
            if (log.isDebugEnabled()) {
                log.writeDebug("doSqlExecute END execute() }");
            }
		} catch (SQLException ex) {
			
	        if (log.isDebugEnabled()) {
	            log.writeDebug("doSqlExecute END execute() }");
	        }
	        
			throw new DBException ("SQLException when execute doSqlExecute", ex);
		}

	}
	
	public void releasePstmt() throws DBException {
		
        if (log.isDebugEnabled()) {
            log.writeDebug("releasePstmt START execute() {");
        }
        
		if (pdst != null) {
			try {
				pdst.close();
			} catch (SQLException ex) {
				throw new DBException ("SQLException when execute releasePstmt", ex);
			}
		}
		
        if (log.isDebugEnabled()) {
            log.writeDebug("releasePstmt END execute() }");
        }
	}
}